package com.angel.web.health;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.angel.core.util.JsonUtil;
import com.angel.core.util.JsonUtils;
import com.angel.core.util.Pager;
import com.angel.health.BuzMedical;
import com.angel.health.BuzMedicalContent;
import com.angel.health.BuzSubjects;
import com.angel.provider.health.BuzMedicalContentProvider;
import com.angel.provider.health.BuzMedicalProvider;
import com.angel.provider.health.BuzSubjectsProvider;

import net.sf.json.util.JSONUtils;

/**
 * @author hanlinxiu
 *
 */

@Controller
@RequestMapping("/medicalContent")
public class BuzMedicalContentController {

	@Autowired
	private BuzMedicalContentProvider provider;
	
	@Autowired
	private BuzSubjectsProvider buzSubjectsProvider;
	
	
	/**
	 * @param modelMap
	 * @param medicalId  体检库ID
	 * @param subjectsId  体检项目ID
	 * @return
	 */
	@RequestMapping("/medicalContent")
	public String findAll(ModelMap modelMap, Integer medicalId, Integer subjectsId){
		
		if(medicalId != null && medicalId == 0)
			medicalId = null;
		
		if(subjectsId != null && subjectsId == 0)
			subjectsId = null;
		
		List<BuzSubjects> list = provider.getAll(medicalId, subjectsId);
		
		modelMap.put("medicalId", medicalId);
		modelMap.put("subjectsId", subjectsId);
		
		modelMap.put("data", JSON.toJSON(list));
		
		return "institutions/medical_content";
		
	}
	
	
	/**
	 * 新增体检指标
	 * 
	 * @param id
	 * @param from
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/toAddMedicalContent")
	public String toAddMedicalContent(Integer id, String from, ModelMap modelMap){
		
		BuzMedicalContent buzMedicalContent = null;
		
		if(id != null){
			buzMedicalContent = provider.findById(id);
			
			BuzSubjects buzSubjects = buzSubjectsProvider.findById(buzMedicalContent.getSubjectsId());
			
			modelMap.put("medicalId", buzSubjects.getMedical().getId());
		}
		
		modelMap.put("from", from);
		modelMap.put("data", buzMedicalContent);
		
		return "institutions/medical_content_add";
	}
	
	
	
	
	/**
	 * 新增，更新体检指标
	 * 
	 * @param buzInstitutions
	 * @return  
	 */
	@RequestMapping("/addMedicalContent")
	@ResponseBody
	public Map<String, Object> addMedicalContent(@ModelAttribute("user") String user, @ModelAttribute("buzMedicalContent")BuzMedicalContent buzMedicalContent){
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		if(buzMedicalContent.getName()==null || buzMedicalContent.getName().trim().equals("")){
			map.put("status", "error");
			map.put("msg", "体检指标名称不能为空!");
			return map;
		}
		
		buzMedicalContent.setEnable(1);
		
		if(buzMedicalContent.getId() == null){
			buzMedicalContent.setCreateBy(user);
			buzMedicalContent.setName(buzMedicalContent.getName().replace("（", "(").replace("）", ")"));
			provider.insert(buzMedicalContent);
		}else{
			buzMedicalContent.setUpdateBy(user);
			provider.updateById(buzMedicalContent);
		}
			
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}
	
	
	@RequestMapping("/getMedicalContent")
	@ResponseBody
	public Map<String, Object> findAll(Integer medicalId, Integer subjectsId){
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<BuzSubjects> list = provider.getAll(medicalId, subjectsId);
		
		map.put("list", list);
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
		
	}
	
	@RequestMapping("/deleteContent")
	@ResponseBody
	public Map<String, Object> deleteContent(BuzMedicalContent buzMedicalContent){
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		provider.delById(buzMedicalContent);
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}
	
	
	
}
